﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using kim.DBUtility;//Please add references
namespace kim.DAL
{
    /// <summary>
    /// 数据访问类:Message
    /// </summary>
    public partial class Message
    {
        public Message()
        { }
        #region  Method
        public DataSet GetNewsPager(string tbname, string FieldKey, int PageCurrent,
int PageSize, string FieldShow, string FieldOrder, string Where)
        {
            SqlParameter[] parameters ={
               new SqlParameter("@tbname",SqlDbType.VarChar,128),
               new SqlParameter("@FieldKey",SqlDbType.VarChar,1000),
               new SqlParameter("@PageCurrent",SqlDbType.Int),
               new SqlParameter("@PageSize",SqlDbType.Int),
               new SqlParameter("@FieldShow",SqlDbType.NVarChar,1000),
               new SqlParameter("@FieldOrder",SqlDbType.NVarChar,1000),
               new SqlParameter("@Where",SqlDbType.NVarChar,1000),
               new SqlParameter("@ItemCount",SqlDbType.Int),
                new SqlParameter("@PageCount",SqlDbType.Int)};

            parameters[0].Value = tbname;
            parameters[1].Value = FieldKey;
            parameters[2].Value = PageCurrent;
            parameters[3].Value = PageSize;
            parameters[4].Value = FieldShow;
            parameters[5].Value = FieldOrder;
            parameters[6].Value = Where;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            return Common.SqlDbHelper.GetDataSetProc("sp_PageView", parameters);
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return kim.DBUtility.DbHelperSQL.GetMaxID("ID", "Message","");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Message");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            return kim.DBUtility.DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(kim.Model.Message model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Message(");
            strSql.Append("MsTitle,MsContent,MsUserName,MsEmail,MsTel,MsAddress,MsPhone,MsGender,MsBudget,MsLfdate,MsModel,MsScore,MsBrand,MsType,AddTime,DesignerID)");
            strSql.Append(" values (");
            strSql.Append("@MsTitle,@MsContent,@MsUserName,@MsEmail,@MsTel,@MsAddress,@MsPhone,@MsGender,@MsBudget,@MsLfdate,@MsModel,@MsScore,@MsBrand,@MsType,@AddTime,@DesignerID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MsTitle", SqlDbType.NVarChar,250),
					new SqlParameter("@MsContent", SqlDbType.NVarChar),
					new SqlParameter("@MsUserName", SqlDbType.NVarChar,250),
					new SqlParameter("@MsEmail", SqlDbType.NVarChar,250),
					new SqlParameter("@MsTel", SqlDbType.NVarChar,250),
					new SqlParameter("@MsAddress", SqlDbType.NVarChar,250),
					new SqlParameter("@MsPhone", SqlDbType.NVarChar,250),
					new SqlParameter("@MsGender", SqlDbType.Int,4),
					new SqlParameter("@MsBudget", SqlDbType.NVarChar,250),
					new SqlParameter("@MsLfdate", SqlDbType.NVarChar,250),
					new SqlParameter("@MsModel", SqlDbType.Int,4),
					new SqlParameter("@MsScore", SqlDbType.Int,4),
					new SqlParameter("@MsBrand", SqlDbType.Int,4),
					new SqlParameter("@MsType", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
                     new SqlParameter("@DesignerID",SqlDbType.Int)};
            parameters[0].Value = model.MsTitle;
            parameters[1].Value = model.MsContent;
            parameters[2].Value = model.MsUserName;
            parameters[3].Value = model.MsEmail;
            parameters[4].Value = model.MsTel;
            parameters[5].Value = model.MsAddress;
            parameters[6].Value = model.MsPhone;
            parameters[7].Value = model.MsGender;
            parameters[8].Value = model.MsBudget;
            parameters[9].Value = model.MsLfdate;
            parameters[10].Value = model.MsModel;
            parameters[11].Value = model.MsScore;
            parameters[12].Value = model.MsBrand;
            parameters[13].Value = model.MsType;
            parameters[14].Value = model.AddTime;
            parameters[15].Value = model.DesignerID;
            object obj = kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(kim.Model.Message model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Message set ");
            strSql.Append("MsTitle=@MsTitle,");
            strSql.Append("MsContent=@MsContent,");
            strSql.Append("MsUserName=@MsUserName,");
            strSql.Append("MsEmail=@MsEmail,");
            strSql.Append("MsTel=@MsTel,");
            strSql.Append("MsAddress=@MsAddress,");
            strSql.Append("MsPhone=@MsPhone,");
            strSql.Append("MsGender=@MsGender,");
            strSql.Append("MsBudget=@MsBudget,");
            strSql.Append("MsLfdate=@MsLfdate,");
            strSql.Append("MsModel=@MsModel,");
            strSql.Append("MsScore=@MsScore,");
            strSql.Append("MsBrand=@MsBrand,");
            strSql.Append("MsType=@MsType,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("DesignerID=@DesignerID");
          
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@MsTitle", SqlDbType.NVarChar,250),
					new SqlParameter("@MsContent", SqlDbType.NVarChar),
					new SqlParameter("@MsUserName", SqlDbType.NVarChar,250),
					new SqlParameter("@MsEmail", SqlDbType.NVarChar,250),
					new SqlParameter("@MsTel", SqlDbType.NVarChar,250),
					new SqlParameter("@MsAddress", SqlDbType.NVarChar,250),
					new SqlParameter("@MsPhone", SqlDbType.NVarChar,250),
					new SqlParameter("@MsGender", SqlDbType.Int,4),
					new SqlParameter("@MsBudget", SqlDbType.NVarChar,250),
					new SqlParameter("@MsLfdate", SqlDbType.NVarChar,250),
					new SqlParameter("@MsModel", SqlDbType.Int,4),
					new SqlParameter("@MsScore", SqlDbType.Int,4),
					new SqlParameter("@MsBrand", SqlDbType.Int,4),
					new SqlParameter("@MsType", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
                      new SqlParameter("@DesignerID", SqlDbType.Int),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.MsTitle;
            parameters[1].Value = model.MsContent;
            parameters[2].Value = model.MsUserName;
            parameters[3].Value = model.MsEmail;
            parameters[4].Value = model.MsTel;
            parameters[5].Value = model.MsAddress;
            parameters[6].Value = model.MsPhone;
            parameters[7].Value = model.MsGender;
            parameters[8].Value = model.MsBudget;
            parameters[9].Value = model.MsLfdate;
            parameters[10].Value = model.MsModel;
            parameters[11].Value = model.MsScore;
            parameters[12].Value = model.MsBrand;
            parameters[13].Value = model.MsType;
            parameters[14].Value = model.AddTime;
            parameters[15].Value = model.DesignerID;

            parameters[16].Value = model.ID;

            int rows = kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Message ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            int rows = kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Message ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public kim.Model.Message GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,MsTitle,MsContent,MsUserName,MsEmail,MsTel,MsAddress,MsPhone,MsGender,MsBudget,MsLfdate,MsModel,MsScore,MsBrand,MsType,AddTime,DesignerID from Message ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            kim.Model.Message model = new kim.Model.Message();
            DataSet ds = kim.DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MsTitle"] != null && ds.Tables[0].Rows[0]["MsTitle"].ToString() != "")
                {
                    model.MsTitle = ds.Tables[0].Rows[0]["MsTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsContent"] != null && ds.Tables[0].Rows[0]["MsContent"].ToString() != "")
                {
                    model.MsContent = ds.Tables[0].Rows[0]["MsContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsUserName"] != null && ds.Tables[0].Rows[0]["MsUserName"].ToString() != "")
                {
                    model.MsUserName = ds.Tables[0].Rows[0]["MsUserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsEmail"] != null && ds.Tables[0].Rows[0]["MsEmail"].ToString() != "")
                {
                    model.MsEmail = ds.Tables[0].Rows[0]["MsEmail"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsTel"] != null && ds.Tables[0].Rows[0]["MsTel"].ToString() != "")
                {
                    model.MsTel = ds.Tables[0].Rows[0]["MsTel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsAddress"] != null && ds.Tables[0].Rows[0]["MsAddress"].ToString() != "")
                {
                    model.MsAddress = ds.Tables[0].Rows[0]["MsAddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsPhone"] != null && ds.Tables[0].Rows[0]["MsPhone"].ToString() != "")
                {
                    model.MsPhone = ds.Tables[0].Rows[0]["MsPhone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsGender"] != null && ds.Tables[0].Rows[0]["MsGender"].ToString() != "")
                {
                    model.MsGender = int.Parse(ds.Tables[0].Rows[0]["MsGender"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MsBudget"] != null && ds.Tables[0].Rows[0]["MsBudget"].ToString() != "")
                {
                    model.MsBudget = ds.Tables[0].Rows[0]["MsBudget"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsLfdate"] != null && ds.Tables[0].Rows[0]["MsLfdate"].ToString() != "")
                {
                    model.MsLfdate = ds.Tables[0].Rows[0]["MsLfdate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MsModel"] != null && ds.Tables[0].Rows[0]["MsModel"].ToString() != "")
                {
                    model.MsModel = int.Parse(ds.Tables[0].Rows[0]["MsModel"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MsScore"] != null && ds.Tables[0].Rows[0]["MsScore"].ToString() != "")
                {
                    model.MsScore = int.Parse(ds.Tables[0].Rows[0]["MsScore"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MsBrand"] != null && ds.Tables[0].Rows[0]["MsBrand"].ToString() != "")
                {
                    model.MsBrand = int.Parse(ds.Tables[0].Rows[0]["MsBrand"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MsType"] != null && ds.Tables[0].Rows[0]["MsType"].ToString() != "")
                {
                    model.MsType = int.Parse(ds.Tables[0].Rows[0]["MsType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DesignerID"] != null && ds.Tables[0].Rows[0]["DesignerID"].ToString() != "")
                {
                    model.DesignerID = int.Parse(ds.Tables[0].Rows[0]["DesignerID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,MsTitle,MsContent,MsUserName,MsEmail,MsTel,MsAddress,MsPhone,MsGender,MsBudget,MsLfdate,MsModel,MsScore,MsBrand,MsType,AddTime,DesignerID ");
            strSql.Append(" FROM Message ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,MsTitle,MsContent,MsUserName,MsEmail,MsTel,MsAddress,MsPhone,MsGender,MsBudget,MsLfdate,MsModel,MsScore,MsBrand,MsType,AddTime,DesignerID ");
            strSql.Append(" FROM Message ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Message ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from Message T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Message";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return kim.DBUtility.DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

